package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import surreal.triviamaster.database.LocalCache;
import surreal.triviamaster.utilities.QuizResults;
import surreal.triviamaster.utilities.TriviaMasterApplication;
import surreal.triviamaster.utilities.TriviaMasterService;
import surreal.triviamaster.utilities.Utilities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Results extends Activity{
	private LocalCache db;

	@Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.results);
	    
	    //getting the db, getting the quiz results
	    db = ((TriviaMasterApplication) getApplication()).getLocalCache();
	    QuizResults qr = db.getQuizResults();
	    Log.d("Results", "Results " +qr.toString());
	    //Display GUI
	    ((TextView) findViewById(R.id.correct)).setText("You got " + qr.getNumCorrect() + 
	    		" out of " + qr.getNumQuestions());
	    ((TextView) findViewById(R.id.points)).setText("You scored " + qr.getPoints() + " points");
	    
	    if(qr.getNumFlagged()>0) {
	    	((TextView) findViewById(R.id.flagged)).setText("You flagged " + qr.getNumFlagged());
	    } else {
	    	((TextView) findViewById(R.id.flagged)).setText("");
	    } 
	    
	    //Flush the results to server
	    //we don't reallllly care if we don't get these results to the db, so there is not network check
    	Intent intent = new Intent(TriviaMasterApplication.ACTION_FLUSH, null, Results.this, TriviaMasterService.class);
		startService(intent);
	    	    
	    //home button
	    Button home = (Button) findViewById(R.id.home);
	    home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(Results.this, TriviaMasterActivity.class);
            	intent.putExtra("fromApp", true);
	  	      	startActivity(intent);
            }
        });
	    
	  //replay button
	    Button replay = (Button) findViewById(R.id.replay);
	    replay.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {  
            	if(Utilities.isNetworkAvailable(Results.this)) {
	            	//getting the new quiz questions from server
	    			Intent intent = new Intent(TriviaMasterApplication.ACTION_GET_QUESTIONS, null, Results.this, TriviaMasterService.class);
	    			startService(intent);
	    			
	    			//loading the new game activity
	            	intent = new Intent(Results.this, LoadingScreen.class);
		  	      	startActivity(intent);
            	} else {
            		Toast.makeText(Results.this, TriviaMasterApplication.NETWORK_ERROR, 5000);
            	}      	
            }
        });
	}
	
	@Override
	public void onBackPressed() {
	   return;
	}
}
